package cn.iocoder.yudao.module.ride.service.training;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ride.controller.admin.training.vo.*;
import cn.iocoder.yudao.module.ride.dal.dataobject.training.TrainingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ride.dal.mysql.training.TrainingMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.ride.enums.ErrorCodeConstants.*;

/**
 * 司机培训课程 Service 实现类
 *
 * @author 小胡同学
 */
@Service
@Validated
public class TrainingServiceImpl implements TrainingService {

    @Resource
    private TrainingMapper trainingMapper;

    @Override
    public Long createTraining(TrainingSaveReqVO createReqVO) {
        // 插入
        TrainingDO training = BeanUtils.toBean(createReqVO, TrainingDO.class);
        trainingMapper.insert(training);

        // 返回
        return training.getId();
    }

    @Override
    public void updateTraining(TrainingSaveReqVO updateReqVO) {
        // 校验存在
        validateTrainingExists(updateReqVO.getId());
        // 更新
        TrainingDO updateObj = BeanUtils.toBean(updateReqVO, TrainingDO.class);
        trainingMapper.updateById(updateObj);
    }

    @Override
    public void deleteTraining(Long id) {
        // 校验存在
        validateTrainingExists(id);
        // 删除
        trainingMapper.deleteById(id);
    }

    @Override
        public void deleteTrainingListByIds(List<Long> ids) {
        // 删除
        trainingMapper.deleteByIds(ids);
        }


    private void validateTrainingExists(Long id) {
        if (trainingMapper.selectById(id) == null) {
            throw exception(TRAINING_NOT_EXISTS);
        }
    }

    @Override
    public TrainingDO getTraining(Long id) {
        return trainingMapper.selectById(id);
    }

    @Override
    public PageResult<TrainingDO> getTrainingPage(TrainingPageReqVO pageReqVO) {
        return trainingMapper.selectPage(pageReqVO);
    }

}